// API Debug Helper
export async function debugApiCall() {
  const apiUrl = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:9000';
  
  console.log('🔍 API Debug Information:');
  console.log('API URL:', apiUrl);
  console.log('Current Origin:', window.location.origin);
  
  // Test 1: Basic fetch
  console.log('\n1. Testing basic fetch to /health:');
  try {
    const response = await fetch(`${apiUrl}/health`, {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
      },
    });
    
    console.log('Response Status:', response.status);
    console.log('Response Headers:', Object.fromEntries(response.headers.entries()));
    
    if (response.ok) {
      const data = await response.json();
      console.log('Response Data:', data);
    } else {
      console.error('Response Error:', await response.text());
    }
  } catch (error) {
    console.error('Fetch Error:', error);
  }
  
  // Test 2: With credentials
  console.log('\n2. Testing fetch with credentials:');
  try {
    const response = await fetch(`${apiUrl}/api/v1/admin/blockchain/status`, {
      method: 'GET',
      credentials: 'include',
      headers: {
        'Content-Type': 'application/json',
      },
    });
    
    console.log('Response Status:', response.status);
    console.log('Response OK:', response.ok);
  } catch (error) {
    console.error('Fetch Error:', error);
  }
  
  // Test 3: Check if it's a network issue
  console.log('\n3. Network diagnostics:');
  console.log('Navigator Online:', navigator.onLine);
  console.log('Protocol:', window.location.protocol);
  
  // Recommendations
  console.log('\n📝 Troubleshooting:');
  if (apiUrl.includes('localhost') && window.location.hostname !== 'localhost') {
    console.error('❌ Backend URL is localhost but frontend is not running on localhost!');
    console.error('   Update NEXT_PUBLIC_API_URL to the actual backend URL');
  }
  if (window.location.protocol === 'https:' && apiUrl.startsWith('http:')) {
    console.error('❌ Mixed content: HTTPS page trying to access HTTP API');
    console.error('   Either use HTTPS for backend or access frontend via HTTP');
  }
}